﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;
using AspNetCs.Config;

public partial class Users_CreateFindFamily : BasePage
{
    FindFamilyBLL bll = new FindFamilyBLL();
    protected AspNetCs.Bll.Temple tempBll = new Temple();

    private int UserId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        //CheckUserExit("Login.aspx","请您登录!",true);
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateFindFamily.aspx", "请您登录", true);
        UserId = Base_GetUserInfo.UserId;
        if (!IsPostBack)
        {
            int Point = (int)Base_GetUserInfo.Point;
            if (Point < configInfo.abscondence)
            {
                //ResultStr("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateAncestralHall.Aspx", "您的可用点数:" + Point + ",创建宗祠需要" + inf.ancestralhallPoint+ "点,请您及时充值");
                ResultStr("Default.aspx", "您的可用点数:" + Point + ",发布海内外寻亲需要" + configInfo.abscondence + "点,请您及时充值");
            }
            BindKindsOfDataControl();
            MusicBLL musicBLL = new MusicBLL();
            musicBLL.BindMusic(ddlMusic);
        }
        //t = ddlMuType.SelectedValue;
        //ArrayList list = new ArrayList();
        //DataTable dt = salBll.GetNetMuIdDataTable(" and SaleType=" + (int)Channel_Type.memorial + "  and  UserId=0");
        //foreach (DataRow dr in dt.Rows)
        //{
        //    list.Add(dr["Number"]);
        //}
        //DefaultValue = bll.RndNum(list, RndLen);
        //this.txtNetMuId.Text = DefaultValue;
    }

    private void BindKindsOfDataControl()
    {
        EnumDescription[] Sex_Ed = EnumDescription.GetFieldTexts(typeof(Sex_Enum), EnumDescription.SortType.Rank);
        EnumDescription[] Ed = EnumDescription.GetFieldTexts(typeof(TimeType_enum), EnumDescription.SortType.Rank);


        this.Radio_Sex.DataTextField = "EnumDisplayText";
        this.Radio_Sex.DataValueField = "EnumDisplayText";
        this.Radio_Sex.DataSource = Sex_Ed;
        this.Radio_Sex.DataBind();
        this.Radio_Sex.SelectedIndex = 0;
        this.Radio_Sex.RepeatColumns = 5;

        this.DD_TimeType.DataTextField = "EnumDisplayText";
        this.DD_TimeType.DataValueField = "EnumDisplayText";
        this.DD_TimeType.DataSource = Ed;
        this.DD_TimeType.DataBind();
        this.DD_TimeType.SelectedIndex = 0;

    }
    public FindFamilyModel getModel()
    {
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateNetMu.Aspx", "请您登录", true);
        FindFamilyModel model = new FindFamilyModel();
        DateTime RegisterTime = DateTime.Now;
        Users_Model user = Base_GetUserInfo;
        int CreateId = 0;
        CreateId = user.UserId;
        int IsCheck = 0;
        int IsDel = 0;
        int AcceeCount = 0;
        int TempleId = StringHelper.isNumber_int(Request["templateId"]);
        int MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);
        model.ItemName = 0;
        model.PreName = txtPreName.Text;
        model.PostfixName = txtPostfixName.Text;
        model.OtherName = Txt_otherName.Text;
        model.Sex = Radio_Sex.SelectedValue;
        model.Age = StringHelper.isNumber_int(txtAge.Text);
        model.BirthType = DD_TimeType.SelectedValue;
        model.Birthday = Txt_birthyear.Text;
        if (model.BirthType == TimeType_enum.pub.ToString())
        {
            model.Birthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
            if (StringHelper.isDatetime_Bool(model.Birthday))
            {
                model.DepartBirthday = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.Birthday));
            }
        }
        else
        {
            model.DepartBirthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
        }
        model.LostPlace = txtLostPlace.Text;
        model.Nation = Txt_Race.Text;
        model.Country = txtCountry.Text;
        model.BirthPlace = Txt_place.Text;
        model.Work = Txt_Occu.Text;
        model.Phone = txtPhone.Text;
        model.Telephone = txtTelephone.Text;
        model.Email = txtEmail.Text;
        model.Qq = txtQQ.Text;
        model.ContactAddress = txtContactAddress.Text;
        model.LostPerInfo = WebEditor1.Text;
        model.RegisterTime = RegisterTime;
        model.CreateId = CreateId;
        model.IsCheck = IsCheck;
        model.IsDel = IsDel;
        model.Photo = Txt_UpImg.Text;
        model.AcceeCount = AcceeCount;
        model.TempleId = TempleId;
        model.MusicId = MusicId;
        model.Title = StringHelper.HtmlEncrypt(StringHelper.RemoveHtml(Find_Title.Text));
        return model;
    }

    protected void btnCreate_Click(object sender, EventArgs e)
    {
        if (StringHelper.HtmlEncrypt(StringHelper.RemoveHtml(Find_Title.Text)) == "")
        {
            Response.Write("<script>alert('标题不能为空！');location.href='CreateFindFamily.aspx';</script>");
            Response.End();
        }
        FindFamilyModel model = getModel();
        bll.AddFamilyInfo(model);
        DrePoint();
        ResultStr("FindFamilyList.aspx", "添加成功!");
    }

    /// <summary>
    /// 扣除点数
    /// </summary>
    protected void DrePoint()
    {
        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        if (UserId != 0)
        {
            if ((TemplatePoint + configInfo.abscondence) <= Base_GetUserInfo.Point)
            {
                Temple_Model TmpMd = TempleBll.CreateNew.GetModel(TempId);
                //userBll.DrePoint(TemplatePoint, UserId);
                Point_Log_Model model = new Point_Log_Model();
                model.NetIp = StringHelper.Ip();
                model.Point = TemplatePoint + configInfo.abscondence;
                if (TemplatePoint != 0)
                {
                    model.Readme = "发布海内外寻亲消息扣除点数:" + configInfo.abscondence + "点,选择模板[" + TmpMd.TempleName + "]扣除" + TemplatePoint + "点";
                }
                else
                {
                    model.Readme = "发布海内外寻亲消息扣除点数:" + configInfo.abscondence + "点,选择了免费模板[" + TmpMd.TempleName + "]";
                }

                model.UserId = this.UserId;
                model.UserType = (int)UserTypeEnum.User;
                model.Addtime = DateTime.Now;

                Point_Log_Bll.CreateNew.Add(model);
            }
            else
            {
                Response.Write("<script>alert('无法创建,用户点数不足(创建需要" + (TemplatePoint + configInfo.abscondence) + "点,您只有" + Base_GetUserInfo.Point + "点)!');location.href='CreateFindFamily.aspx';</script>");
                return;
            }
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/CreateFindFamily.Aspx", "请您登录", true);
        }
    }
}
